/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package th.manager;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import th.dao.AkaunFavDAO;
import th.model.AkaunFav;

/**
 *
 * @author hasnor
 */
public class AkaunFavManager {
    
    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    
    @Inject
    AkaunFavDAO akaunFavDAO;
    
    public AkaunFav getAkaunFav (String no_kp) {
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select a from AkaunFav a " +
                "where noKp = :noKP");      
        query.setString("noKP", no_kp);
        return (AkaunFav) query.uniqueResult();
     }
    
    public AkaunFav getAkaunFavByNoAcc (String noAcc) {
        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "select a from AkaunFav a " +
                "where a.noAkaunFav = :noAkaun");      
        query.setString("noAkaun", noAcc);
        return (AkaunFav) query.uniqueResult();
     }
    
    @Transactional
    public void saveAccountFav(AkaunFav akaunFav) {
       akaunFavDAO.save(akaunFav);
    } 
    
    
//     public void updateAccFavByNoAcc (String noAcc) {
//       Session session = sessionProvider.get();
//         StringBuilder sb = new StringBuilder("Select a from AkaunFav a where a.noAkaunFav = :akaun");
//         Query query = session.createQuery(sb.toString()).setParameter("akaun", noAcc);
//         
//         AkaunFav akaun = (AkaunFav) query.uniqueResult();
//         if (akaun != null) {
//             Transaction tx = session.beginTransaction();
//             //akaunFavDAO.delete(akaun);
//             akaunFavDAO.saveOrUpdate(akaun);
//             tx.commit();                
//         }  
//     }
    
    @Transactional
    public void updateAccFavByNoAcc (AkaunFav akaunFavEdit) {
         Session session = sessionProvider.get();
         String noAcc = akaunFavEdit.getNoAkaunFav();
         StringBuilder sb = new StringBuilder("Select a from AkaunFav a where a.noAkaunFav = :akaun");
         Query query = session.createQuery(sb.toString()).setParameter("akaun", noAcc);
         
         AkaunFav akaun = (AkaunFav) query.uniqueResult();
         
         if (akaun != null) {
             akaun.setNoAkaunFav(akaunFavEdit.getNoAkaunFav());
             akaun.setNicknameFav(akaunFavEdit.getNicknameFav());
             
             akaunFavDAO.saveOrUpdate(akaun);     
         }  
     }
     
    public void deleteAkaun(String noAkaun) {
         Session session = sessionProvider.get();
         StringBuilder sb = new StringBuilder("Select a from AkaunFav a where a.noAkaunFav = :akaun");
         Query query = session.createQuery(sb.toString()).setParameter("akaun", noAkaun);
         
         AkaunFav akaun = (AkaunFav) query.uniqueResult();
         if (akaun != null) {
             Transaction tx = session.beginTransaction();
             akaunFavDAO.delete(akaun);
             tx.commit();                
         }         
     }
}
